package com.it.controller;

import com.it.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 消费者无需使用Service层，直接调用远程
 * <p>
 * RestTemplate模板类，通过 配置类 注册到Spring，直接调用内部方法
 */
@RestController
@RequestMapping("/consumer")
public class DeptConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    //服务提供端url
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
    //ribbon使用的是服务的名称去访问的
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/add")
    public boolean addDept(@RequestBody Dept dept) {
        /**
         * 参数一：URL
         * 参数二：post请求参数
         * 参数三：返回值类型的字节码文件
         */
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add/", dept, boolean.class);
    }

    @RequestMapping("/get/{id}")
    public Dept getDept(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @RequestMapping("/getAll")
    public List<Dept> getAllDept() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/getAll", List.class);
    }

}
